﻿package igz.fleaxy.net.rss 
{	
	import igz.fleaxy.util.Convert;
	/**
	 * @author Intelygenz [www.intelygenz.com]
	 * Clase ocupada de generar un item a partir de un XML, tiene en cuenta si es un ATOM o es FEED
	 * 
	 */
	public class RssItem {
		
		public var $Title:String;
		public var $Link:String;
		public var $Description:String;
		public var $Author:String;
		public var $Category:String;
		public var $Comments:String;
		public var $Enclosure:XML;
		public var $Guid:String;
		public var $Pubdate:String; 
		public var $Source:String;
		public var $Summary:String;
		public var $Name:String;
		public var $Email:String;
		public var $AlreadyRead:String;
		public var $ManagingEditor:String;
		public var $Content:String;
		public var $Image:XML;

		public var $Updated:String;
		
		private var _Xml:XML;
		public function get $Xml () : XML {
			return _Xml;
		}

		/**
		 * Función que recibe un RSS, detecta de que tipo es y lo transforma en un RSS item
		 * 
		 * @param	p_xml XML con los datos del elemento
		 * @param	p_RssType tipo de rss (mirar en RssTypes.[tab])
		 */
		public function RssItem( p_xml:XML, p_RssType:int ) {
			_Xml = p_xml;
			switch( p_RssType ){
				case RssTypes.$ATOM:
					_ParseAtom( p_xml );
					break;
				case RssTypes.$RSS:
					_ParseRss( p_xml );
					break;
				case RssTypes.$RDF:
					_ParseRdf( p_xml );
					break;
			}
		}

		private function _ParseRss( p_xml:XML ) : void {
			var fieldName : String;
			var xmlNode : XML;
			for each ( xmlNode in  p_xml.* ) {

				fieldName = _GetFieldName( xmlNode.name().toString() );
				if ( this.hasOwnProperty(fieldName) ) {
					if ( fieldName == "$Image" || fieldName == "$Enclosure" ) { // TODO: Mejorar para hacerlo que funcione para cualquier nodo XML
						this[fieldName] = XML(xmlNode);
					}
					else {
						this[fieldName] = Convert.$ToString( xmlNode );
					}
				}
				//else {
//trace("igz.fleaxy.net.rss.RssItem._ParseRss No se encontró el campo ["+fieldName+"]")
				//}

				/* TODO: Encontrar algún RSS que tenga un XML en author
				 * 
					if ( xmlNode.name() == "author" ) {
						for each (var xmlAuth:XML in  xmlNode.elements()) {	
							if ( this.hasOwnProperty( xmlNode.localName() ) )
								this[xmlNode.localName()] = xml;
					}
				}*/
			}
			
			/*if ( $ManagingEditor != null ) {
				$Name = $ManagingEditor.substr( 0, $ManagingEditor.indexOf(" ") );
			}*/
		}

		private function _ParseAtom( p_xml:XML ) : void {
			$Summary = "";
			$Description = "";

			var xmlNode : XML;
			var fieldName : String;
			for each ( xmlNode in  p_xml.* ) {
				
				fieldName = _GetFieldName( xmlNode.localName().toString() );

				/*if(( xml.localName() == "author" )){
					for each (var xmlAuth:XML in  xml.elements()) {				
						if ( this.hasOwnProperty( xml.localName() ) )
							this[xml.localName()] = xml;
					}
				}*/
				if ( this.hasOwnProperty( fieldName ) ) {
					//if ( xmlNode.localName() == "content" ) {
						//this[fieldName] = Convert.$ToString( xmlNode.toString );
						this[fieldName] = xmlNode;
					//}
				}
				
				//si el link viene vacio, miro el atributo href
				if ( xmlNode.localName() == "link" ) {
					$Link = xmlNode.@href ;
				}
			}

			if ( $Description == null || $Description == "" ) {
				$Description = $Content;
			}


		}
		
		private function _ParseRdf( p_xml:XML ) : void {
			var fieldName : String;
			var xmlNode : XML;
			for each ( xmlNode in  p_xml.* ) {

				
				fieldName = xmlNode.name().toString();
				fieldName = _GetFieldName( fieldName.substring( fieldName.lastIndexOf( "::" ) + 2 ) );
				if ( this.hasOwnProperty(fieldName) ) {
trace( "RssRDF->Encontrado: " +  fieldName );					
					if ( fieldName == "$Image" || fieldName == "$Enclosure" ) { // TODO: Mejorar para hacerlo que funcione para cualquier nodo XML
						this[fieldName] = XML(xmlNode);
					}
					else {
						this[fieldName] = Convert.$ToString( xmlNode );
trace( "*** Valor: " + this[ fieldName ] );						
					}
				}else {
					if ( fieldName == "$Date" ) {
						this["$Pubdate"] = Convert.$ToString( xmlNode );
trace( "RssRDF->Encontrado: " +  fieldName );					
trace( "*** Valor: " + this[ "$Pubdate" ] );						
					}
				}
				//else {
//trace("igz.fleaxy.net.rss.RssItem._ParseRss No se encontró el campo ["+fieldName+"]")
				//}

				/* TODO: Encontrar algún RSS que tenga un XML en author
				 * 
					if ( xmlNode.name() == "author" ) {
						for each (var xmlAuth:XML in  xmlNode.elements()) {	
							if ( this.hasOwnProperty( xmlNode.localName() ) )
								this[xmlNode.localName()] = xml;
					}
				}*/
			}
			
			/*if ( $ManagingEditor != null ) {
				$Name = $ManagingEditor.substr( 0, $ManagingEditor.indexOf(" ") );
			}*/
		}
		

		private function _GetFieldName ( p_xmlNodeName:String ) : String {
			var fieldName : String = "$" 
								   + p_xmlNodeName.substr(0, 1).toUpperCase()
								   + p_xmlNodeName.substr(1).toLowerCase()
								   ;
//trace( "RssItem->" + fieldName );								   
			return fieldName;
		}
	}
}